package com.ruoyi.demo05;

import java.io.*;
import java.net.Socket;
import java.util.Properties;

import static com.ruoyi.demo05.Server.USER_FILE_PATH;

/**
 * @Author Husp
 * @Date 2023/12/10 18:56
 */
public class ServerReaderThread extends Thread {

    private Socket socket;

    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        // 处理注册和登录
        try {
            //输入流接收消息
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            while (true) {
                int type = dataInputStream.readInt();
                switch (type) {
                    case 1:
                        //1、处理注册
                        register();
                        break;
                    case 2:
                        //2、处理登录
                        login();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("网络发生了故障，请稍后在试~");
        }
    }

    private void register() throws IOException {
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        String username = dataInputStream.readUTF();
        String password = dataInputStream.readUTF();
        String result = "注册成功";
        if (exist(username)) {
            result = "注册失败，用户名已经被占用~";
        } else {
            //保存用户名和密码
            Properties properties = new Properties();
            properties.setProperty(username, password);
            properties.store(new FileWriter(USER_FILE_PATH, true), "");
        }
        //输出流到客户端
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataOutputStream.writeUTF(result);
        dataOutputStream.flush();
    }


    private void login() throws IOException {
        DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
        String username = dataInputStream.readUTF();
        String password = dataInputStream.readUTF();
        String result = "登录成功";
        File file = new File(USER_FILE_PATH);
        if (file.exists()) {
            Properties properties = new Properties();
            properties.load(new FileReader(file));
            String passwordCache = properties.getProperty(username);
            //校验密码
            if (passwordCache == null) {
                result = "用户名或密码不存在！";
            }else {
                if (!password.equals(passwordCache)) {
                    result = "密码错误";
                }
            }
        } else {
            result = "当前用户不存在";
        }
        //输出流到客户端
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataOutputStream.writeUTF(result);
        dataOutputStream.flush();
    }

    private boolean exist(String username) throws IOException {
        //读取配置文件
        File file = new File(USER_FILE_PATH);
        //判断文件是否存在
        if (file.exists()) {
            Properties properties = new Properties();
            properties.load(new FileReader(file));
            // 判断用户是否存在
            if (properties.containsKey(username)) {
                return true;
            }
        }
        return false;
    }
}
